package cn.toolck.service.impl;

import cn.toolck.dao.LagouTokenDao;
import cn.toolck.pojo.LagouToken;
import cn.toolck.user.service.LagouTokenService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Optional;

@Service
public class LagouTokenServiceImpl implements LagouTokenService {
    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Override
    public LagouToken insertOrUpdate(String email, String token) {
        LagouToken lagouToken = null;
        Optional<LagouToken> lagouTokenOptional = findOneByEmail(email);
        if (lagouTokenOptional.isPresent()) {
            lagouToken = lagouTokenOptional.get();
        } else {
            lagouToken = new LagouToken();
            lagouToken.setEmail(email);
        }
        lagouToken.setToken(token);
        return lagouTokenDao.save(lagouToken);
    }

    @Override
    public Optional<LagouToken> findOneByEmail(String email) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        Example<LagouToken> example = Example.of(lagouToken);
        return lagouTokenDao.findOne(example);
    }

    @Override
    public Optional<LagouToken> findOneByToken(String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Example<LagouToken> example = Example.of(lagouToken);
        return lagouTokenDao.findOne(example);
    }


}
